#    Copyright 2012 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPWeb.
#    COOPWeb is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPWeb is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPWeb.  If not, see <http://www.gnu.org/licenses/>.

import os
import datetime
import shutil

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopweb/djangoproject/include', '/opt/coopweb/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime
#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/coopweb/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
from django.core.management import setup_environ
import djangoproject
from djangoproject import settings
setup_environ(settings)
from djangoproject.models import *
#********************Setup psycopg2 and django section********************

from django.template import Context, loader
from django.http import HttpResponse
from django import forms
import os
import datetime
import djangoproject.settings


















def UpdateAlarmTableTemplate(Username,Alarms):
    FileName='/opt/coopweb/djangoproject/templates/dynamic_alarmtable_' + Username + '.html'
    Total = 0
    ReplacementBlockList=[]
    for i in range(0, len(Alarms)):
        RecentStatus=Alarms[i].AlarmStatus
        AlarmEventOpenIndicator=bool(Alarms[i].CurrentEventid)
        if(AlarmEventOpenIndicator):
            EventTableData = ('<td>{{form.AlarmAcknowledgeIndicator' + str(i) + '}} ' + '</td>' +
                              '<td>{{form.AlarmCloseIndicator' + str(i) + '}} ' + '</td>'+
                              '<td>{{form.AlarmEventDescription' + str(i) + '}}<br>' + "Start Time: " + str(Alarms[i].CurrentEventStartDateTime) +
                                                                                     "<br>Acknowledge Time:  " + str(Alarms[i].CurrentEventAcknowledgeDateTime) + '</td>')
        else:
            EventTableData = """<td>&nbsp</td>
                                <td>&nbsp</td>
                                <td>&nbsp<br>No Current Events</td>"""
        
        ReplacementBlockList.append('<tr><td>{{form.UpdateIndicator' + str(i) + '}}</td>' +
                                    '<td>{{form.id' + str(i) + '}}</td>' +
                                    '<td>{{form.Alarmid' + str(i) + '}}</td>' +
                                    '<td>' + Alarms[i].SubstationName + '</td>' +
                                    '<td>' + RecentStatus  + '</td>' +
                                    '<td>{{form.AlarmArmIndicator' + str(i) + '}}</td>' +
                                    '<td>{{form.AlarmDisarmIndicator' + str(i) + '}}</td>' +
                                    '<td>{{form.AlarmMessage' + str(i) + '}}</td>' + 
                                    EventTableData
    )
    Total = Total + 1
    ReplacementBlockString=''.join(ReplacementBlockList)
    FinalTemplate=AlarmTableFormTemplate.replace('{{Repeating Block}}', ReplacementBlockString)
    try:
        os.remove(FileName)
    except OSError:
        pass
    TemplateFile=open(FileName, 'w')
    TemplateFile.write(FinalTemplate)
    TemplateFile.close()






AlarmTableFormTemplate="""
<!--
#    Copyright 2012 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPWeb.
#    COOPWeb is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPWeb is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPWeb.  If not, see <http://www.gnu.org/licenses/>.
**************************************************************************
THIS FILE WAS DYNAMICALLY GENERATED BY THE DYNAMIC TEMPLATE INCLUDE
MODIFICATIONS TO THIS FILE WILL BE PERIODICALLY OVERWRITTEN!!!!!
MODIFY THIS TEMPLATE IN THE COOPWeb_DYNAMICTEMPLATE.PY FILE
**************************************************************************
-->

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>COOPWeb Alarm Table Form</title>
  </head>
  <body>
	<b>COOPWeb Alarm Table Form </b> &nbsp&nbsp&nbsp&nbsp <small><a href="/coopweb/">Main Menu</a></small>
<form method="post" action="">
 <table>
<tr><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td>&nbsp</td><td><input value="Submit" type="submit"></td></tr>

<tr><th>Update</th><th>ID</th><th>Alarm ID</th><th>Substation</th><th>Status</th><th>Arm</th><th>Disarm</th><th>Message</th><th>Acknowledge and Disarm</th><th>Close and Arm</th><th>Event Notes</th></tr>

{{Repeating Block}}

</table>

</form>

  </body>
</html>
"""




